package com.zr.lads.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.lads.pojo.User;
import com.zr.lads.service.FuncService;
import com.zr.lads.service.LoginService;
import com.zr.lads.util.PrintWriterUtil;

import net.sf.json.JSONArray;

/**
 * 处理用户登录时的请求
 * @author tang
 *
 */
@Controller
@Scope(value="prototype")
public class LoginAction extends ActionSupport implements SessionAware,ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String account;  //用户名
	private String password; //用户密码
	
	@Resource
	private LoginService loginService;
	@Resource
	private FuncService funcService;
	
	private HttpServletResponse resp;
	private Map<String, Object> session;

	public String getAccount() {
		return account;
	}
	
	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	/**
	 * 验证登录信息
	 */
	public void login() {
		User u = loginService.isExistUser(account, password);
		PrintWriter pw = PrintWriterUtil.getPW(resp);
		if (u != null) {
			session.put("currentUser", u);
			pw.write("1");
		} else {
			pw.write("0");
		}
	}
	/**
	 * 获取当前用户功能列表
	 */
	public void showFuncs(){
		User u = (User) session.get("currentUser");
		String s = funcService.getCurrentUserFuncs(u.getUId());
		JSONArray currentFuncs = funcService.getCurrentUserFuncsByParentId(-1, s);
		PrintWriter pw = PrintWriterUtil.getPW(resp);
		pw.write(currentFuncs.toString());
	}
	
	@Override
	public void setServletResponse(HttpServletResponse resp) {
		this.resp = resp;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
